package com.test5.liang.controller;


import com.test5.liang.pojo.User;
import com.test5.liang.pojo.examination;
import com.test5.liang.service.UserService;
import com.test5.liang.service.examinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/exam")
public class examController {


    @Autowired
    examinationService examinationService;

    @Autowired
    UserService userService;



    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 20:19 2021/12/17
     * @Param
     * @return
     **/
    @RequestMapping("/test")
    public String test(Model model){



        return "exampage";
    }


    public int getRandomNum(int type){
        Random r = new Random();
        switch (type){
            case 0:{
                return r.nextInt(9)+1;
            }
            case 1:{
                return r.nextInt(99)+1;
            }
            case 2:{
                return r.nextInt(999)+1;
            }
            default:{
                return 0;
            }
        }
    }



    public List getloadList(int n,int type,int numtype,List list){
        Random r = new Random();

        for (int k=0;k<n;k++){
            //使用map封装一条算术
            HashMap<String, String> map = new HashMap<>();
            int addone = getRandomNum(numtype);
            map.put("addone",String.valueOf(addone));
            switch (type){
                case 0:map.put("operation","+"); break;
                case 1:map.put("operation","-"); break;
                case 2:map.put("operation","x"); break;
                case 3:map.put("operation","÷"); break;
                case 4:{
                    int i = r.nextInt(4);
                    switch (i){
                        case 0:map.put("operation","+"); break;
                        case 1:map.put("operation","-"); break;
                        case 2:map.put("operation","x"); break;
                        case 3:map.put("operation","÷"); break;
                    }
                }
            }
            int addtwo = getRandomNum(numtype);
            map.put("addtwo",String.valueOf(addtwo));
            list.add(map);
        }
        return list;
    }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 14:03 2021/12/18
     * @Param
     * @return
     **/
    @RequestMapping("/beginexam")
    public String beginexam(@RequestParam("type")Integer type, @RequestParam("numtype")Integer numtype, Model model, HttpSession httpSession){
        Random r = new Random();
        //提数
        int n = 10;
        int questionNum = 30;
        httpSession.setAttribute("questionNum",questionNum);
        int mode = (int) httpSession.getAttribute("mode");
        model.addAttribute("mode",mode);

        //声明集合装载随机数
        List<HashMap> list1 = new ArrayList<>();
        List<HashMap> list2 = new ArrayList<>();
        List<HashMap> list3 = new ArrayList<>();
        //List<HashMap> list4 = new ArrayList<>();

        list1=getloadList(n,type,numtype,list1);
        list2=getloadList(n,type,numtype,list2);
        list3=getloadList(n,type,numtype,list3);
       // list4=getloadList(n,type,numtype,list4);
        for (HashMap map : list1) {
            System.out.println(map.toString());
        }
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);
        //model.addAttribute("list4",list4);
        httpSession.setAttribute("type",type);
        httpSession.setAttribute("numtype",numtype);
        return "exampage";
    }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 15:30 2021/12/18
     * @Param 选择试题模式
     * @return
     **/
    @RequestMapping("/examchoose")
    public String examchoose(@RequestParam("username")String username,HttpSession httpSession) {
        httpSession.setAttribute("username",username);
        return "hellopage";
    }

    @RequestMapping("/hellopage")
    public String hellopage() {
        return "hellopage";
    }


    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 23:47 2021/12/18
     * @Param
     * @return
     **/
    @RequestMapping("/uploadexam")
    public String uploadexam(@RequestParam("numOne")List<String> numOne,
                             @RequestParam("numTwo")List<String> numTwo,
                             @RequestParam("numThree")List<String> numThree,
                             @RequestParam("numFour")List<String> numFour,
                             HttpSession httpSession) {

        int score = 0;

        for (int index=0;index<numOne.size();index++){
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            if (pattern.matcher(numFour.get(index)).matches()&& !numFour.get(index).equals("")) {
                if (numTwo.get(index).equals("+")) {
                    if (Integer.parseInt(numOne.get(index)) + Integer.parseInt(numThree.get(index)) == Integer.parseInt(numFour.get(index))) {
                        score += 4;
                    }
                } else if (numTwo.get(index).equals("-")) {
                    if (Integer.parseInt(numOne.get(index)) - Integer.parseInt(numThree.get(index)) == Integer.parseInt(numFour.get(index))) {
                        score += 4;
                    }
                } else if (numTwo.get(index).equals("x")) {
                    if (Integer.parseInt(numOne.get(index)) * Integer.parseInt(numThree.get(index)) == Integer.parseInt(numFour.get(index))) {
                        score += 4;
                    }
                } else if (numTwo.get(index).equals("÷")) {
                    if (Integer.parseInt(numOne.get(index)) / Integer.parseInt(numThree.get(index)) == Integer.parseInt(numFour.get(index))) {
                        score += 4;
                    }
                }
            }
        }
        //插入成绩数据
        examination examination = new examination();
        examination.setCreateTime(new Date());
        examination.setExamScore(score);
         //通过Session获取试题类型
        int type = (int)httpSession.getAttribute("type");
        int numtype = (int)httpSession.getAttribute("numtype");
        //int timelimit = (int)httpSession.getAttribute("timelimit");
        int mode = (int)httpSession.getAttribute("mode");
        examination.setTimelimit(mode);
        String username = (String)httpSession.getAttribute("username");
        User user = userService.findUserByName(username);
        examination.setUserId(user.getUserId());
        int examtypeid = numtype*5+type+1;
        examination.setExamTypeId(examtypeid);
        int questionNum = (int)httpSession.getAttribute("questionNum");
        examination.setQuestionNum(questionNum);
        examination.setStatus(1);
        examination.setTimeLength(60);
        examinationService.insertExamination(examination);

        return "dailyRegistersuccess";
    }


     /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 14:20 2021/12/19
     * @Param 选择限时或非限时模式
     * @return
     **/
     @RequestMapping("/chooseMode")
     public String uploadexam(@RequestParam("type")Integer type,Model model,HttpSession httpSession) {

         httpSession.setAttribute("mode",type);
         return "examChoose";
     }

}
